package com.kum.controller;

import com.kum.annotations.RefreshUserScriptCache;
import com.kum.annotations.UserId;
import com.kum.domain.R;
import com.kum.domain.dto.SysScriptDto;
import com.kum.domain.entity.SysScript;
import com.kum.service.SysScriptService;
import com.kum.service.SysUserScriptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @version V1.0
 * @Package com.kum.controller
 * @auhter 枯木Kum
 * @date 2021/6/16-10:03 AM
 * <p>...</p>
 */

@RestController
@RequestMapping("/script")
public class SysScriptController {

    @Autowired
    private SysScriptService scriptService;
    @Autowired
    private SysUserScriptService userScriptService;

    @PostMapping("/add")
    public R add(@RequestBody SysScriptDto scriptDto, @UserId String userId) {
        scriptService.add(scriptDto);
        return R.ok();
    }

    @PostMapping("/save")
    public R save(@RequestBody SysScript script, @UserId String userId) {
        if (script.getId() == null) {
            script.setScriptAuthorId(userId);
        }
        scriptService.save(script);
        return R.ok();
    }

    @GetMapping("/get/own")
    public R getOwn(@UserId String userId) {
        return R.ok(scriptService.get(userId));
    }

    @RefreshUserScriptCache
    @GetMapping("/change/status/{scriptId}/{status}")
    public R changeStatus(@PathVariable String scriptId,@PathVariable String status,@UserId String userId){
        if(userScriptService.changeStatus(userId,scriptId,status) > 0){
            return R.ok();
        }
        return R.fail();
    }

}
